<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AngularJS&middot;Hello AngularJS</title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.css">
    <!--<link rel="stylesheet" href="http://cdn.bootcss.com/prettify/r298/prettify.min.css"/>-->
    <link rel="stylesheet" href="../static/js/prettify/prettify.css"/>
    <style>
        .demo{
            margin-bottom:10px;
            border-bottom:1px solid #1b926c;
        }
    </style>
</head>
<body>

 <div class="container">
     <div class="row">
         <nav class="navbar navbar-default navbar-static-top">
             <div class="container-fluid">
                 <div class="navbar-header">
                     <a class="navbar-brand" href="../index.html">首页</a>
                 </div>
                 <ul class="nav navbar-nav">
                    <li class="active"><a href="javascript:void(0)">Link</a></li>
                    <li><a href="javascript:void(0)">Link2</a></li>
                 </ul>
             </div>
         </nav>
     </div>
     <h1 class="page-title">AngularJS $http Get请求,获取json数据</h1>
     <div ng-app="myApp">
         <div class="row demo">
             <h3>Demo1: 字符串转大写</h3>
<p>JSON数据</p>
<pre>
{
    "posts": [
        {
            "title": "Hello ,title1",
            "desc": "Hello ,this is desceiption"
        },
        {
            "title": "Hello ,title2",
            "desc": "Hello ,this is desceiption"
        },
        {
            "title": "Hello ,title3",
            "desc": "Hello ,this is desceiption"
        },
        {
            "title": "Hello ,title4",
            "desc": "Hello ,this is desceiption"
        },
        {
            "title": "Hello ,title5",
            "desc": "Hello ,this is desceiption"
        },
        {
            "title": "Hello ,title6",
            "desc": "Hello ,this is desceiption"
        }
    ]
}
</pre>
<p>angularjs代码</p>
<pre>
    var myApp=angular.module("myApp",[]);

    myApp.controller("PostsController",function($scope,$http){
        $http.get('data/posts.json').
                success(function(data, status, headers, config) {
                    console.log(data.posts);
                    $scope.posts = data.posts;
                }).
                error(function(data, status, headers, config) {
                    // log error
                    alert("get json error");
                });
    });
</pre>
             <br/>
             <br/>
             <br/>
             <h5>输出结果:</h5>
             <div class="text-center" ng-controller="PostsController">
                 <div class="row">
                     <div class="col-md-6"><h3>Title</h3></div>
                     <div class="col-md-6"><h3>Description</h3></div>
                 </div>
                 <div class="row" ng-repeat="post in posts">
                     <div class="col-md-6">{{post.title}}</div>
                     <div class="col-md-6">{{post.desc}}</div>
                 </div>
             </div>
         </div>
     </div>
 </div>
</body>
</html>
<script src="http://cdn.bootcss.com/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
<script src="http://cdn.bootcss.com/angular.js/1.3.8/angular.js" type="text/javascript"></script>
<script src="../static/js/prettify/prettify.min.js" type="text/javascript"></script>
<script>
    /**
     * pretiffy美化代码
     */
    $(function() {
        $('pre').addClass('prettyprint linenums').attr('style', 'overflow:auto');
        window.prettyPrint && prettyPrint();
    });
</script>
<script>
    var myApp=angular.module("myApp",[]);

    myApp.controller("PostsController",function($scope,$http){
        $http.get('data/posts.json').
                success(function(data, status, headers, config) {
                    console.log(data.posts);
                    $scope.posts = data.posts;
                }).
                error(function(data, status, headers, config) {
                    // log error
                    alert("get json error");
                });
    });


</script>
